package com.gycode.vpush.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gycode.vpush.constant.CommonConstant;
import com.gycode.vpush.dto.base.ResultDTO;
import com.gycode.vpush.dto.request.SendMessageDTO;
import com.gycode.vpush.dto.request.WebSocketSendDTO;
import com.gycode.vpush.enums.WebSocketEnum;
import com.gycode.vpush.utils.ResultDTOUtil;
import com.gycode.vpush.utils.mq.ProducerUtil;
import com.gycode.vpush.entity.SendMessage;
import com.gycode.vpush.entity.SendMessageUser;
import com.gycode.vpush.mapper.SendMessageMapper;
import com.gycode.vpush.mapper.SendMessageUserMapper;
import com.gycode.vpush.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @author PC
 */
@Service
@Slf4j
public class MessageServiceImpl implements MessageService {

    @Autowired
    private ProducerUtil producerUtil;

    @Autowired
    private SendMessageMapper sendMessageMapper;

    @Autowired
    private SendMessageUserMapper sendMessageUserMapper;

    @Override
    public ResultDTO sendMessage(SendMessageDTO sendMessageDTO) {
        Integer msgType = sendMessageDTO.getType();
        saveMessage(sendMessageDTO);
        WebSocketSendDTO webSocketSendDTO = sendMessageDTO.getWebSocketSendDTO();
        //websocket
        if (WebSocketEnum.MSG_TYPE1.getCode().equals(msgType)) {
            if (WebSocketEnum.SOCKET_TYPE1.getCode().equals(webSocketSendDTO.getIsPointTime())) {
                //发送即时消息
                log.info("发送即时消息");
                producerUtil.sendMsgBroadcast(CommonConstant.MQ_TAG_WEBSOCKET_TIMELY_MSG,
                        JSONObject.toJSONBytes(sendMessageDTO),
                        UUID.randomUUID().toString());

            } else if (WebSocketEnum.SOCKET_TYPE0.getCode().equals(webSocketSendDTO.getIsPointTime())) {
                log.info("发送定时消息");
                if (ObjectUtils.isEmpty(webSocketSendDTO.getPointTime())) {
                    return ResultDTOUtil.error(-1, "定时消息未设置时间");
                }

            }

        }
        //短信
        else if (WebSocketEnum.MSG_TYPE2.getCode().equals(msgType)) {
            log.info("发送短信。。。");
        } else {
            log.info("发送邮件。。。");
        }
        return ResultDTOUtil.success();
    }

    private void saveMessage(SendMessageDTO sendMessageDTO) {
        WebSocketSendDTO webSocketSendDTO = sendMessageDTO.getWebSocketSendDTO();
        SendMessage sendMessage = new SendMessage();
        sendMessage.setContent(sendMessageDTO.getContent());
        sendMessage.setType(sendMessageDTO.getType());
        sendMessage.setIsPointTime(webSocketSendDTO.getIsPointTime() == 1);
        sendMessage.setPointTime(webSocketSendDTO.getPointTime());
        sendMessage.setPath(webSocketSendDTO.getPath());
        sendMessage.setTitle(sendMessageDTO.getTitle());
        sendMessageMapper.insert(sendMessage);
        List<String> userIdList = webSocketSendDTO.getUserIdList();
        for (String userId : userIdList) {
            SendMessageUser sendMessageUser = new SendMessageUser();
            sendMessageUser.setSendMessageId(sendMessage.getId());
            sendMessageUser.setUserId(userId);
            sendMessageUser.setStatus(false);
            sendMessageUserMapper.insert(sendMessageUser);
        }
        webSocketSendDTO.setMessageId(sendMessage.getId());
    }
}
